package com.myMinistry.gui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.myMinistry.R;
import com.myMinistry.provider.MinistryContract.EntryType;
import com.myMinistry.provider.MinistryContract.Householder;
import com.myMinistry.provider.MinistryContract.Literature;
import com.myMinistry.provider.MinistryContract.LiteratureType;
import com.myMinistry.provider.MinistryContract.Publisher;
import com.myMinistry.provider.MinistryService;

public class DialogFrag extends DialogFragment {
	public static final int ACTIVE_PUBLISHERS = 1;
	public static final int INACTIVE_PUBLISHERS = 2;
	public static final int CREATE_PUBLISHER = 3;
	public static final int EDIT_PUBLISHER = 4;
	public static final int ENTRY_TYPES = 5;
	public static final int ACTIVE_HOUSEHOLDERS = 6;
	public static final int CREATE_HOUSEHOLDER = 7;
	public static final int TYPES_OF_LITERATURE = 8;
	public static final int LITERATURE_BY_TYPE = 9;
	public static final int CREATE_LITERATURE = 10;
	public static final int NUMBER_PICKER = 11;
	public static final int NUMBER_PICKER_WITH_ZERO = 12;
	public static final int TYPES_OF_LITERATURE_WITH_REMOVE = 13;
	
    public static DialogFrag newInstance(String title, int whichDialog) {
    	DialogFrag frag = new DialogFrag();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("whichDialog", whichDialog);
        frag.setArguments(args);
        return frag;
    }
	
    public static DialogFrag newInstance(String title, int whichDialog, int publisherID) {
    	DialogFrag frag = new DialogFrag();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("whichDialog", whichDialog);
        args.putInt("publisherID", publisherID);
        frag.setArguments(args);
        return frag;
    }
	
    public static DialogFrag newInstance(String title, int whichDialog, String literatureName) {
    	DialogFrag frag = new DialogFrag();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("whichDialog", whichDialog);
        args.putString("literatureName", literatureName);
        frag.setArguments(args);
        return frag;
    }
	
    public static DialogFrag newInstance(String title, int whichDialog, int publisherID, int typeID) {
    	DialogFrag frag = new DialogFrag();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putInt("whichDialog", whichDialog);
        args.putInt("publisherID", publisherID);
        args.putInt("typeID", typeID);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        switch(getArguments().getInt("whichDialog")) {
        case ACTIVE_PUBLISHERS: {
        	final int NEW_PUBLISHER_ID = -1;
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickPublisher> publisherList = new ArrayList<QuickPublisher>();
    		ArrayAdapter<QuickPublisher> adapter;
    		
    		/** Add the default to add a new publisher on the fly */
    		publisherList.add(new QuickPublisher(NEW_PUBLISHER_ID,getActivity().getApplicationContext().getString(R.string.addNewPublisher)));
    		
    		/** Get active publishers from DB */
    		database.openReadable();
    		final Cursor publishers = database.fetchActivePublishers();
	        while(publishers.moveToNext())
        		publisherList.add(new QuickPublisher(publishers.getInt(publishers.getColumnIndex(Publisher._ID)),publishers.getString(publishers.getColumnIndex(Publisher.NAME))));
	        publishers.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickPublisher>(getActivity(), R.layout.simple_list_item_1, R.id.text1, publisherList);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickPublisher pub = publisherList.get(which);
						/** Create new publisher selected */
						if(pub.getID() == NEW_PUBLISHER_ID)
							createNewPublisherShowDialog();
						/** An actual publisher was selected */
						else
							saveSharedPrefs(pub.getID(), pub.toString());
					}})
				.create();
	        }
        case CREATE_PUBLISHER: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_text, null);
        	
        	/** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Get the input value */
            			EditText editText = (EditText) view.findViewById(R.id.text1);
            			String newName = editText.getText().toString();
                        if(newName.length() > 0) {
                        	/** Create a new publisher */
                        	MinistryService database = new MinistryService(getActivity());
        					ContentValues values = new ContentValues();
        					values.put(Publisher.NAME, newName);
        					values.put(Publisher.ACTIVE, MinistryService.ACTIVE);
        					database.openWritable();
                        	long newID = database.createPublisher(values);
                        	database.close();
						    if(newID > 0) {
                            	/** A publisher was created */
							    saveSharedPrefs((int)newID, newName);
						    }
						}
                    }})
                .create();
	        }
        case EDIT_PUBLISHER: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_publisher_editor, null);
        	MinistryService database = new MinistryService(getActivity());
            database.openReadable();
            Cursor publisherInfo = database.fetchPublisher(getArguments().getInt("publisherID"));
            if(publisherInfo.moveToFirst()) {
            	QuickPublisher quickPub = new QuickPublisher(publisherInfo.getInt(publisherInfo.getColumnIndex(Publisher._ID)), publisherInfo.getString(publisherInfo.getColumnIndex(Publisher.NAME)));
            	EditText editBox = (EditText) view.findViewById(R.id.publisherName);
            	editBox.setText(quickPub.toString());
            }
            publisherInfo.close();
            database.close();
            
            /** Build our Dialog */
            return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),R.style.dialogTheme))
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Get the input values */
            			EditText editText = (EditText) view.findViewById(R.id.publisherName);
            			String newName = editText.getText().toString();
                        if(newName.length() > 0) {
                        	/** Edit the publisher */
                        	MinistryService database = new MinistryService(getActivity());
        					ContentValues values = new ContentValues();
        					values.put(Publisher.NAME, newName);
        					values.put(Publisher.ACTIVE, MinistryService.ACTIVE);
        					database.openWritable();
                        	database.updatePublisher(getArguments().getInt("publisherID"), values);
                        	database.close();
						}
                    }})
                .create();
	        }
        case ENTRY_TYPES: {
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickEntry> list = new ArrayList<QuickEntry>();
    		ArrayAdapter<QuickEntry> adapter;
    		
    		/** Get active publishers from DB */
    		database.openReadable();
    		final Cursor types = database.fetchEntryTypes();
	        while(types.moveToNext())
        		list.add(new QuickEntry(types.getInt(types.getColumnIndex(EntryType._ID)),types.getString(types.getColumnIndex(EntryType.NAME)),types.getInt(types.getColumnIndex(EntryType.RV)),types.getInt(types.getColumnIndex(EntryType.STUDY))));
	        types.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickEntry>(getActivity(), R.layout.simple_list_item_1, R.id.text1, list);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickEntry entry = list.get(which);
						if(getActivity() instanceof TimeEditor)
							((TimeEditor) getActivity()).updateEntryType(entry.getID(),entry.toString(),entry.isStudyOrRV());
					}})
				.create();
	        }
        case ACTIVE_HOUSEHOLDERS: {
        	final int NEW_HOUSEHOLDER_ID = -1;
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickEntry> list = new ArrayList<QuickEntry>();
    		ArrayAdapter<QuickEntry> adapter;
    		
    		/** Add the default to add a new publisher on the fly */
    		list.add(new QuickEntry(NEW_HOUSEHOLDER_ID,getActivity().getApplicationContext().getString(R.string.addNewHouseholder),0,0));
    		
    		/** Get active publishers from DB */
    		database.openReadable();
    		final Cursor householders = database.fetchActiveHouseholders();
	        while(householders.moveToNext())
        		list.add(new QuickEntry(householders.getInt(householders.getColumnIndex(Householder._ID)),householders.getString(householders.getColumnIndex(Householder.NAME)),0,0));
	        householders.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickEntry>(getActivity(), R.layout.simple_list_item_1, R.id.text1, list);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickEntry entry = list.get(which);
						/** Create new publisher selected */
						if(entry.getID() == NEW_HOUSEHOLDER_ID)
							createNewHouseholderShowDialog();
						/** An actual publisher was selected */
						else if(getActivity() instanceof TimeEditor)
							((TimeEditor) getActivity()).updateHouseholder(entry.getID(), entry.toString());
					}})
				.create();
	        }
        case CREATE_HOUSEHOLDER: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_text, null);
        	
        	/** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Get the input value */
            			EditText editText = (EditText) view.findViewById(R.id.text1);
            			String newName = editText.getText().toString();
                        if(newName.length() > 0) {
                        	/** Create a new householder */
                        	MinistryService database = new MinistryService(getActivity());
        					ContentValues values = new ContentValues();
        					values.put(Householder.NAME, newName);
        					values.put(Householder.ACTIVE, MinistryService.ACTIVE);
        					database.openWritable();
                        	long newID = database.createHouseholder(values);
                        	database.close();
						    if(newID > 0) {
                            	/** A householder was created */
						    	if(getActivity() instanceof TimeEditor)
									((TimeEditor) getActivity()).updateHouseholder((int)newID, newName);
						    	else if(getActivity() instanceof Householders)
									((Householders) getActivity()).updateList();
						    }
						}
                    }})
                .create();
	        }
        case TYPES_OF_LITERATURE: {
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickEntry> list = new ArrayList<QuickEntry>();
    		ArrayAdapter<QuickEntry> adapter;
    		
    		/** Get types of literature from DB */
    		database.openReadable();
    		final Cursor records = database.fetchTypesOfLiterature();
	        while(records.moveToNext())
        		list.add(new QuickEntry(records.getInt(records.getColumnIndex(LiteratureType._ID)),records.getString(records.getColumnIndex(LiteratureType.NAME)), 0, 0));
	        records.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickEntry>(getActivity(), R.layout.simple_list_item_1, R.id.text1, list);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickEntry entry = list.get(which);
						if(getActivity() instanceof TimeEditor)
							((TimeEditor) getActivity()).typeOfLiteratureSelected(entry.getID(), entry.toString());
					}})
				.create();
	        }
        case TYPES_OF_LITERATURE_WITH_REMOVE: {
        	final int REMOVE_ID = -1;
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickEntry> list = new ArrayList<QuickEntry>();
    		ArrayAdapter<QuickEntry> adapter;
    		
    		/** Add the default to add a new publisher on the fly */
    		list.add(new QuickEntry(REMOVE_ID,getActivity().getApplicationContext().getString(R.string.removeWithOperator) + " " + getArguments().getString("literatureName"),0,0));
    		
    		/** Get types of literature from DB */
    		database.openReadable();
    		final Cursor records = database.fetchTypesOfLiterature();
	        while(records.moveToNext())
        		list.add(new QuickEntry(records.getInt(records.getColumnIndex(LiteratureType._ID)),records.getString(records.getColumnIndex(LiteratureType.NAME)), 0, 0));
	        records.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickEntry>(getActivity(), R.layout.simple_list_item_1, R.id.text1, list);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickEntry entry = list.get(which);
						/** Create new publisher selected */
						if(entry.getID() == REMOVE_ID) {
							if(getActivity() instanceof TimeEditor)
								((TimeEditor) getActivity()).removeLiteratureSelected();
						}
						/** An actual publisher was selected */
						else if(getActivity() instanceof TimeEditor)
							((TimeEditor) getActivity()).typeOfLiteratureSelected(entry.getID(), entry.toString());
					}})
				.create();
	        }
        case LITERATURE_BY_TYPE: {
        	final int NEW_ID = -1;
    		final MinistryService database = new MinistryService(getActivity());
    		final ArrayList<QuickEntry> list = new ArrayList<QuickEntry>();
    		ArrayAdapter<QuickEntry> adapter;
    		
    		/** Add the default to add a new publisher on the fly */
    		list.add(new QuickEntry(NEW_ID,getActivity().getApplicationContext().getString(R.string.createNew),0,0));
    		
    		/** Get literature of type from DB */
    		database.openReadable();
    		final Cursor records = database.fetchLiteratureByType(getArguments().getInt("typeID"));
	        while(records.moveToNext())
        		list.add(new QuickEntry(records.getInt(records.getColumnIndex(Literature._ID)),records.getString(records.getColumnIndex(Literature.NAME)), 0, 0));
	        records.close();
	        database.close();
	        
	        adapter = new ArrayAdapter<QuickEntry>(getActivity(), R.layout.simple_list_item_1, R.id.text1, list);
	        
	        /** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setAdapter(adapter, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						QuickEntry entry = list.get(which);
						/** Create new publisher selected */
						if(entry.getID() == NEW_ID)
							createNewLiteratureShowDialog();
						/** An actual publisher was selected */
						else if(getActivity() instanceof TimeEditor)
							((TimeEditor) getActivity()).updateLiteratureSelected(entry.getID(), entry.toString());
					}})
				.create();
	        }
        case CREATE_LITERATURE: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.edit_text, null);
        	
        	/** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setInverseBackgroundForced(true)
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Get the input value */
            			EditText editText = (EditText) view.findViewById(R.id.text1);
            			String newName = editText.getText().toString();
                        if(newName.length() > 0) {
                        	/** Create a new householder */
                        	MinistryService database = new MinistryService(getActivity());
        					ContentValues values = new ContentValues();
        					values.put(Literature.NAME, newName);
        					values.put(Literature.PUBLISHER_ID, getArguments().getInt("publisherID"));
        					values.put(Literature.TYPE_OF_LIERATURE_ID, getArguments().getInt("typeID"));
        					values.put(Literature.WEIGHT, 1);
        					values.put(Literature.ACTIVE, MinistryService.ACTIVE);
        					values.put(Literature.SORT_ORDER, 0);
        					database.openWritable();
                        	long newID = database.createLiterature(values);
                        	database.close();
						    if(newID > 0) {
                            	/** A entry was created */
						    	if(getActivity() instanceof TimeEditor)
									((TimeEditor) getActivity()).updateLiteratureSelected((int)newID, newName);
						    }
						}
                    }})
                .create();
	        }
        case NUMBER_PICKER: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_numberpicker, null);
        	Button btn_up = (Button)view.findViewById(R.id.numberpicker_up);
        	final Button btn_down = (Button)view.findViewById(R.id.numberpicker_down);
        	final TextView input = (TextView)view.findViewById(R.id.numberpicker_input);
        	input.setText("1");
        	btn_down.setEnabled(false);
        	btn_up.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int num = Integer.valueOf(input.getText().toString());
					num++;
					if(num > 1)
						btn_down.setEnabled(true);
					input.setText(String.valueOf(num));
				}
			});
        	btn_down.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int num = Integer.valueOf(input.getText().toString());
					num--;
					if(num == 1)
						btn_down.setEnabled(false);
					input.setText(String.valueOf(num));
				}
			});
        	
        	/** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			int num = Integer.valueOf(input.getText().toString());
                        if(num > 0) {
                        	if(getActivity() instanceof TimeEditor)
                        		((TimeEditor) getActivity()).updateLiteratureSelectedCount(num);
						}
                    }})
                .create();
	        }
        case NUMBER_PICKER_WITH_ZERO: {
        	final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_numberpicker, null);
        	Button btn_up = (Button)view.findViewById(R.id.numberpicker_up);
        	final Button btn_down = (Button)view.findViewById(R.id.numberpicker_down);
        	final TextView input = (TextView)view.findViewById(R.id.numberpicker_input);
        	input.setText("0");
        	btn_up.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int num = Integer.valueOf(input.getText().toString());
					num++;
					input.setText(String.valueOf(num));
				}
			});
        	btn_down.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					int num = Integer.valueOf(input.getText().toString());
					num--;
					input.setText(String.valueOf(num));
				}
			});
        	
        	/** Build our Dialog */
        	return new AlertDialog.Builder(getActivity())
				.setTitle(getArguments().getString("title"))
				.setView(view)
            	.setNegativeButton(R.string.cancel, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			/** Do nothing */
            		}})
            	.setPositiveButton(R.string.save, new AlertDialog.OnClickListener() {
            		@Override
                    public void onClick(DialogInterface dialog, int which) {
            			int num = Integer.valueOf(input.getText().toString());
                        if(num > 0) {
                        	if(getActivity() instanceof TimeEditor)
                        		((TimeEditor) getActivity()).updateRVCount(num);
						}
                    }})
                .create();
	        }
        }
        return null;
    }
    
    private void saveSharedPrefs(int publisherID, String publisherName) {
    	SharedPreferences sharedPref = getActivity().getSharedPreferences(Summary.MY_PREFS, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPref.edit();
		/** Save publisher info to the shared preferences. */
		editor.putInt(Summary.MY_PREFS_KEY_PUBLISHER_ID, publisherID);
		editor.putString(Summary.MY_PREFS_KEY_PUBLISHER_NAME, publisherName);
		/** Commit preferences */
		editor.commit();
		
		/** Do we need to update the parent of the action bar? */
		if(getActivity() instanceof Summary)
			((Summary) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof TimeEditor)
			((TimeEditor) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof TimeEntries)
			((TimeEntries) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof Householders)
			((Householders) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof HouseholderEditor)
			((HouseholderEditor) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof Publishers)
			((Publishers) getActivity()).updatePublisher(publisherID,publisherName);
		else if(getActivity() instanceof PublisherEditor)
			((PublisherEditor) getActivity()).updatePublisher(publisherID,publisherName);
    }
    
    private void createNewPublisherShowDialog() {
    	if(getActivity() instanceof Summary)
			((Summary) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof TimeEditor)
			((TimeEditor) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof TimeEntries)
			((TimeEntries) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof Householders)
			((Householders) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof HouseholderEditor)
			((HouseholderEditor) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof Publishers)
			((Publishers) getActivity()).createNewPublisherShowDialog();
    	else if(getActivity() instanceof PublisherEditor)
			((PublisherEditor) getActivity()).createNewPublisherShowDialog();
    }
    
    private void createNewHouseholderShowDialog() {
    	if(getActivity() instanceof TimeEditor)
    		((TimeEditor) getActivity()).createNewHouseholderShowDialog();
    }
    
    private void createNewLiteratureShowDialog() {
    	if(getActivity() instanceof TimeEditor)
    		((TimeEditor) getActivity()).createNewLiteratureShowDialog();
    }
    
    public static class QuickPublisher {
    	private final int id;
    	private final String name;
    	
    	public QuickPublisher(int _id, String _name) {
    		id = _id;
    		name = _name;
    	}
    	
    	@Override
		public String toString() {
    		return name;
    	}
    	
    	public int getID() {
    		return id;
    	}
    }
    
    public static class QuickEntry {
    	private final int id;
    	private final String name;
    	private final int rv;
    	private final int study;
    	
    	public QuickEntry(int _id, String _name, int _rv, int _study) {
    		id = _id;
    		name = _name;
    		rv = _rv;
    		study = _study;
    	}
    	
    	@Override
		public String toString() {
    		return name;
    	}
    	
    	public int getID() {
    		return id;
    	}
    	
    	public boolean isStudyOrRV() {
    		return (rv == 1 || study == 1) ? true : false;
    	}
    }
}